//Given a positive integer num, write a function which returns True if num is a 
//perfect square else False. 
//
// Follow up: Do not use any built-in library function such as sqrt. 
//
// 
// Example 1: 
// Input: num = 16
//Output: true
// Example 2: 
// Input: num = 14
//Output: false
// 
// 
// Constraints: 
//
// 
// 1 <= num <= 2^31 - 1 
// 
// Related Topics Math Binary Search 👍 1800 👎 215


package leetcode.editor.en;

public class _367_ValidPerfectSquare {
    public static void main(String[] args) {
        Solution solution = new _367_ValidPerfectSquare().new Solution();
        boolean perfectSquare = solution.isPerfectSquare1(9);
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public boolean isPerfectSquare1(int num) {
            if (num==0) {
                return true;
            }
            if (num==1) {
                return true;
            }
            float l = 0f, r = num/2f;
            while (l<r){
                float mid = l + (r - l) / 2;
                if (mid<num/mid) {
                    l = (int)mid+1;
                }else {
                    r=(int)mid;
                }
            }
            return r == num/r;
        }

        public boolean isPerfectSquare(int num) {
            if (num == 0) {
                return true;
            }
            if (num == 1) {
                return true;
            }
            float l = 0f, r = num/2f;
            while (l<r){
                float mid = l + (r - l) / 2;
                if (mid>num/mid) {
                    r = (int)mid;
                }else if (mid == num/mid){
                    return true;
                }else {
                    l = (int)mid+1;
                }
            }
            return l == num / l;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}